import 'package:flutter/material.dart';
import '../services/weather_service.dart';
import 'detail_screen.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final TextEditingController _controller = TextEditingController();
  Map<String, dynamic>? weatherData;
  String city = '北京';

  @override
  void initState() {
    super.initState();
    fetchWeather();
  }

  Future<void> fetchWeather([String? searchCity]) async {
    final data = await WeatherService.fetchWeather(searchCity ?? city);
    setState(() {
      weatherData = data;
      if (searchCity != null) city = searchCity;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('天气速览')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            TextField(
              controller: _controller,
              decoration: InputDecoration(
                hintText: '请输入城市名称',
                prefixIcon: Icon(Icons.search),
                border: OutlineInputBorder(borderRadius: BorderRadius.circular(8)),
              ),
              onSubmitted: (value) {
                if (value.isNotEmpty) fetchWeather(value);
              },
            ),
            const SizedBox(height: 20),
            weatherData == null
                ? const CircularProgressIndicator()
                : Card(
                    child: ListTile(
                      leading: Icon(Icons.cloud, size: 40),
                      title: Text('当前城市：$city'),
                      subtitle: Text('天气：${weatherData!['now']['text']}\n温度：${weatherData!['now']['temp']}°C'),
                      onTap: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) => DetailScreen(city: city, weatherData: weatherData!),
                          ),
                        );
                      },
                    ),
                  ),
          ],
        ),
      ),
    );
  }
}

class DetailScreen extends StatelessWidget {
  final String city;
  final Map<String, dynamic> weatherData;

  const DetailScreen({
    Key? key,
    required this.city,
    required this.weatherData,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final now = weatherData['now'];
    return Scaffold(
      appBar: AppBar(title: Text('$city 天气详情')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('城市：$city', style: TextStyle(fontSize: 22)),
            SizedBox(height: 12),
            Text('天气：${now['text']}', style: TextStyle(fontSize: 18)),
            SizedBox(height: 8),
            Text('温度：${now['temp']}°C', style: TextStyle(fontSize: 18)),
            // 可以根据需要添加更多天气信息
          ],
        ),
      ),
    );
  }
}

class WeatherService {
  static Future<Map<String, dynamic>?> fetchWeather(String city) async {
    // 这里写你的获取天气的逻辑
  }

  static Future<Map<String, dynamic>?> fetchRealtimeWeather(double lat, double lng) async {
    // 这里写你的获取实时天气的逻辑
  }
}